package com.cgtong.android.mvp.presenter;

import com.cgtong.android.bean.ActivityDetailBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.mvp.contract.ActivityDetailContract;
import com.cgtong.android.mvp.model.ActivityDetailModel;
import com.google.gson.Gson;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import org.json.JSONObject;

/**
 * Created by Sky.Bing on 2017/10/25.
 */

public class ActivityDetailPresenter implements ActivityDetailContract.Presenter {
    private ActivityDetailContract.View mView;
    private ActivityDetailContract.Model mModel;

    public ActivityDetailPresenter(ActivityDetailContract.View view) {
        this.mView = view;
        mModel = new ActivityDetailModel();
    }

    @Override
    public void getActivityDetail(int activityId) {
        mModel.getActivityDetail(activityId, new SimpleCallBack<String>() {
            @Override
            public void onError(ApiException e) {
                if (e.getCode() == ConstantManager.ACCESS_TOKEN_EXPIRED
                        || e.getCode() == ConstantManager.ACCESS_USER_FORBIDDEN) {
                    return;
                }
                mView.getDetailError(e);
            }

            @Override
            public void onSuccess(String activityDetailStr) {
                try {
                    JSONObject jsonObj = new JSONObject(activityDetailStr);
                    int code = jsonObj.getInt("code");
                    String msg = jsonObj.getString("msg");
                    if (code == ConstantManager.ACCESS_TOKEN_EXPIRED
                            || code == ConstantManager.ACCESS_USER_FORBIDDEN) {
                        return;
                    }
                    if (jsonObj.get("data") instanceof JSONObject) {
                        ActivityDetailBean activityDetailBean = new Gson().fromJson(jsonObj.getString("data"), ActivityDetailBean.class);
                        if (code == 0) {
                            mView.showDetail(activityDetailBean);
                        } else {
                            mView.getDetailError(new ApiException(new Exception(msg), code));
                        }
                    } else {
                        mView.getDetailError(new ApiException(new Exception(msg), code));
                    }
                } catch (Exception e) {
                    mView.getDetailError(new ApiException(e, ApiException.PARSE_ERROR));
                }
            }
        });
    }

    @Override
    public void postActivityVote(int activityId, String voteItems) {
        mModel.postActivityVote(activityId, voteItems, new SimpleCallBack<Object>() {
            @Override
            public void onError(ApiException e) {
                if (e.getCode() == ConstantManager.ACCESS_TOKEN_EXPIRED
                        || e.getCode() == ConstantManager.ACCESS_USER_FORBIDDEN) {
                    return;
                }
                mView.postActivityVoteError(e);
            }

            @Override
            public void onSuccess(Object o) {
                mView.postActivityVoteSuccess();
            }
        });
    }
}